<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
//取IP、port、序列号
    $deval=@$_GET["device_id"];
	$deip=@$_GET["ip"];
	$deport=@$_GET["port"];
	$detime=@$_GET["time"];
	$string = "\n device_id:".$deval."deip:".$deip."deport".$deport."detime".$detime."\n";
    file_put_contents("test.log", $string, FILE_APPEND);
	if($deval && $deip && $deport && $detime)
	{		
		if(abs(strtotime($detime) - strtotime(date('YmdHis')))>180)
		{
		    echo "{\"device_id\":\"".$deval."\",\"time\":\"".date('YmdHis')."\",\"cmd_code\":\"KQ_TIME\"}";
//		    $string = "{\"time\":\"".date('YmdHis')."\",\"cmd_code\":\"KQ_TIME\"}";
//		    file_put_contents("test.log", $string, FILE_APPEND);
			return;
		}        		
//		$string = "\n device_id:".$deval."deip:".$deip."deport".$deport."detime".$detime."\n";
//        file_put_contents("test.log", $string, FILE_APPEND);
//        if(($deval.length <11) && ($deip.length<16) && ($deport.length<11) && ($detime.length<20))
/*		if(($deval.length >64) || ($deip.length>16) || ($deport.length>8) || ($detime.length>19))
	    {
		    $string = "{\"$deval.length\":\"".$deval.length."\",\"$deip.length\":\"".$deip.length."\"}";
		    file_put_contents("test.log", $string, FILE_APPEND);
			return;	
		}*/
        
		$myconn=@mysql_connect("localhost","root","root");	     
        if(!$myconn)
        {
            die('Could not connect: '.mysql_error());
//			$string = "Could not connect: ".mysql_error();
//            file_put_contents("test.log", $string, FILE_APPEND);
			echo "{\"res\":\"error\"}";
			return;
        }
		
	    $db_selected=mysql_select_db("dwaccessDB",$myconn);
	    if(!$db_selected)
        {
            die("Can't use table : ".mysql_error());
//	        $string = "Can't use table : ".mysql_error();
//            file_put_contents("test.log", $string, FILE_APPEND);
			echo "{\"res\":\"error\"}";
			return;
        }
		
   		$strSql="SELECT COUNT(*) FROM gatesetting WHERE controlerNum = \"".$deval."\";";
		
		$result=mysql_query($strSql,$myconn);
		$dbResult = mysql_fetch_array($result);
//		$string = "\n strSql:".$strSql."result : ".$dbResult[0]."\n";
//		file_put_contents("test.log", $string, FILE_APPEND);
        
		if($dbResult[0])
		{
		    $strSql="UPDATE gatesetting SET stat= 'online',lasttime='".date('Y-m-d H:i:s')."',IP='".$deip."',PORT='".$deport."' WHERE controlerNum = '".$deval."';";
	        $result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);
//		    $string = "\n strSql:".$strSql."\n";
//		    file_put_contents("test.log", $string, FILE_APPEND);
		}else
		{
		    for($i=0;$i<4;$i++)
			{
				$strSql="INSERT INTO gatesetting(controlerNum,IP,PORT,stat,lasttime,gatenum) VALUES('".$deval."','".$deip."','".$deport."','online','".date('Y-m-d H:i:s')."','$i');";
				$result=mysql_query($strSql,$myconn);
				@mysql_free_result($result);
			}
//			}$string = "strSql:".$strSql."\n";
//		    file_put_contents("test.log", $string, FILE_APPEND);	
		}
		
	    $strSql="SELECT cmdstr FROM cmdform WHERE (controlerNum =\"0\" OR controlerNum = \"".$deval."\");";
//		file_put_contents("test.log", $strSql, FILE_APPEND);
        $result=mysql_query($strSql,$myconn);
        $strSql="[";
        $i=0;	
		while(($dbResult = mysql_fetch_array($result)) && ($i<200))
	    {
			$strSql .=$dbResult[0].",";
			$i++;
	    }
		if($i==0)
		{
		    $strSql .= "]";
		}else
		{
		    $strSql = substr_replace($strSql,"]",-1,1);
		}
//		file_put_contents("test.log", "\n strSql6".$strSql."i=".$i."\n", FILE_APPEND);
		@mysql_free_result($result);
	    mysql_close($myconn);
		echo $strSql;
		return;        	 		
	}else
	{
//	    file_put_contents("test.log", " _GET[] null \n", FILE_APPEND);
		echo "{\"res\":\"error\"}";
		return;	 
	}
	
?> 
